package com.example.zluserservice.controller;

import com.example.zluserservice.client.RoleFeignClient;
import com.example.zluserservice.common.base.PageResponse;
import com.example.zluserservice.common.base.ResponseResult;
import com.example.zluserservice.common.util.UserThreadLocal;
import com.example.zluserservice.dto.request.RoleReqDTO;
import com.example.zluserservice.dto.response.RoleResDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zhenliu（孙凌岳）
 * @Description 用户服务 - 角色控制器（RPC调用）
 * @Date 2025/6/17 7:23
 * @Version 1.0
 */
@Slf4j
@Api(tags = "用户服务 - 角色控制器（RPC调用）")
@RestController
@RequestMapping("/user/role")
public class RoleController {
    @Resource
    private RoleFeignClient roleFeignClient;

    /**
     *  保存角色
     * @param roleReqDTO 角色DTO对象
     * @return RoleVo
     */
    @PutMapping
    @ApiOperation(value = "添加角色", notes = "创建一个新的角色")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState"})
    public ResponseResult<RoleResDTO> createRole(@RequestBody RoleReqDTO roleReqDTO) {
        roleReqDTO.setCreateBy(UserThreadLocal.getMgtUserId());
        roleReqDTO.setUpdateBy(UserThreadLocal.getMgtUserId());
        RoleResDTO roleVoResult = roleFeignClient.createRole(roleReqDTO);
        return ResponseResult.success(roleVoResult);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{roleId}")
    @ApiOperation(value = "删除角色", notes = "根据角色ID删除指定角色")
    public ResponseResult remove(@PathVariable("roleId") Long roleId) {
        return ResponseResult.success(roleFeignClient.deleteRoleById(roleId));
    }

    /**
     *  修改角色
     * @param roleReqDTO 角色dto对象
     * @return Boolean 是否修改成功
     */
    @PatchMapping
    @ApiOperation(value = "修改角色", notes = "根据角色ID修改角色信息")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState","roleDto.dataScope","roleDto.checkedResourceNos","roleDto.checkedDeptNos","roleDto.id"})
    public ResponseResult<Boolean> updateRole(@RequestBody RoleReqDTO roleReqDTO) {
        roleReqDTO.setUpdateBy(UserThreadLocal.getMgtUserId());
        Boolean flag = roleFeignClient.updateRole(roleReqDTO);
        return ResponseResult.success(flag);
    }

    /***
     *  多条件查询角色分页列表
     * @param roleReqDTO 角色DTO查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return: Page<RoleVo>
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页查询角色", notes = "按条件分页查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleReqDTO"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleResDTO>> findRoleResDTOPage(
            @RequestBody RoleReqDTO roleReqDTO,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<RoleResDTO> roleResDTOPageResponse = roleFeignClient.findRolePage(roleReqDTO, pageNum, pageSize);
        return ResponseResult.success(roleResDTOPageResponse);
    }

    /**
     * 初始化角色信息
     * 角色下拉框
     * @return
     */
    @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉框", notes = "初始化角色列表用于下拉展示")
    ResponseResult<List<RoleResDTO>> initRoles() {
        List<RoleResDTO> roleResDTOList = roleFeignClient.initRoles();
        return ResponseResult.success(roleResDTOList);
    }
}
